home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / email / feedparser.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-13  |  9KB  |  419 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. __all__ = [
  5.     'FeedParser']
  6. import re
  7. from email import errors
  8. from email import message
  9. NLCRE = re.compile('\r\n|\r|\n')
  10. NLCRE_bol = re.compile('(\r\n|\r|\n)')
  11. NLCRE_eol = re.compile('(\r\n|\r|\n)$')
  12. NLCRE_crack = re.compile('(\r\n|\r|\n)')
  13. headerRE = re.compile('^(From |[\\041-\\071\\073-\\176]{1,}:|[\\t ])')
  14. EMPTYSTRING = ''
  15. NL = '\n'
  16. NeedMoreData = object()
  17.  
  18. class BufferedSubFile(object):
  19.     
  20.     def __init__(self):
  21.         self._partial = ''
  22.         self._lines = []
  23.         self._eofstack = []
  24.         self._closed = False
  25.  
  26.     
  27.     def push_eof_matcher(self, pred):
  28.         self._eofstack.append(pred)
  29.  
  30.     
  31.     def pop_eof_matcher(self):
  32.         return self._eofstack.pop()
  33.  
  34.     
  35.     def close(self):
  36.         self._lines.append(self._partial)
  37.         self._partial = ''
  38.         self._closed = True
  39.  
  40.     
  41.     def readline(self):
  42.         if not self._lines:
  43.             if self._closed:
  44.                 return ''
  45.             
  46.             return NeedMoreData
  47.         
  48.         line = self._lines.pop()
  49.         for ateof in self._eofstack[::-1]:
  50.             if ateof(line):
  51.                 self._lines.append(line)
  52.                 return ''
  53.                 continue
  54.         
  55.         return line
  56.  
  57.     
  58.     def unreadline(self, line):
  59.         self._lines.append(line)
  60.  
  61.     
  62.     def push(self, data):
  63.         data = self._partial + data
  64.         self._partial = ''
  65.         parts = NLCRE_crack.split(data)
  66.         self._partial = parts.pop()
  67.         lines = []
  68.         for i in range(len(parts) // 2):
  69.             lines.append(parts[i * 2] + parts[i * 2 + 1])
  70.         
  71.         self.pushlines(lines)
  72.  
  73.     
  74.     def pushlines(self, lines):
  75.         self._lines[:0] = lines[::-1]
  76.  
  77.     
  78.     def is_closed(self):
  79.         return self._closed
  80.  
  81.     
  82.     def __iter__(self):
  83.         return self
  84.  
  85.     
  86.     def next(self):
  87.         line = self.readline()
  88.         if line == '':
  89.             raise StopIteration
  90.         
  91.         return line
  92.  
  93.  
  94.  
  95. class FeedParser:
  96.     
  97.     def __init__(self, _factory = message.Message):
  98.         self._factory = _factory
  99.         self._input = BufferedSubFile()
  100.         self._msgstack = []
  101.         self._parse = self._parsegen().next
  102.         self._cur = None
  103.         self._last = None
  104.         self._headersonly = False
  105.  
  106.     
  107.     def _set_headersonly(self):
  108.         self._headersonly = True
  109.  
  110.     
  111.     def feed(self, data):
  112.         self._input.push(data)
  113.         self._call_parse()
  114.  
  115.     
  116.     def _call_parse(self):
  117.         
  118.         try:
  119.             self._parse()
  120.         except StopIteration:
  121.             pass
  122.  
  123.  
  124.     
  125.     def close(self):
  126.         self._input.close()
  127.         self._call_parse()
  128.         root = self._pop_message()
  129.         if root.get_content_maintype() == 'multipart' and not root.is_multipart():
  130.             root.defects.append(errors.MultipartInvariantViolationDefect())
  131.         
  132.         return root
  133.  
  134.     
  135.     def _new_message(self):
  136.         msg = self._factory()
  137.         if self._cur and self._cur.get_content_type() == 'multipart/digest':
  138.             msg.set_default_type('message/rfc822')
  139.         
  140.         if self._msgstack:
  141.             self._msgstack[-1].attach(msg)
  142.         
  143.         self._msgstack.append(msg)
  144.         self._cur = msg
  145.         self._last = msg
  146.  
  147.     
  148.     def _pop_message(self):
  149.         retval = self._msgstack.pop()
  150.         if self._msgstack:
  151.             self._cur = self._msgstack[-1]
  152.         else:
  153.             self._cur = None
  154.         return retval
  155.  
  156.     
  157.     def _parsegen(self):
  158.         self._new_message()
  159.         headers = []
  160.         for line in self._input:
  161.             if line is NeedMoreData:
  162.                 yield NeedMoreData
  163.                 continue
  164.             
  165.             if not headerRE.match(line):
  166.                 if not NLCRE.match(line):
  167.                     self._input.unreadline(line)
  168.                 
  169.                 break
  170.             
  171.             headers.append(line)
  172.         
  173.         self._parse_headers(headers)
  174.         if self._headersonly:
  175.             lines = []
  176.             while True:
  177.                 line = self._input.readline()
  178.                 if line is NeedMoreData:
  179.                     yield NeedMoreData
  180.                     continue
  181.                 
  182.                 if line == '':
  183.                     break
  184.                 
  185.                 lines.append(line)
  186.             self._cur.set_payload(EMPTYSTRING.join(lines))
  187.             return None
  188.         
  189.         if self._cur.get_content_type() == 'message/delivery-status':
  190.             while True:
  191.                 self._input.push_eof_matcher(NLCRE.match)
  192.                 for retval in self._parsegen():
  193.                     if retval is NeedMoreData:
  194.                         yield NeedMoreData
  195.                         continue
  196.                     
  197.                     break
  198.                 
  199.                 msg = self._pop_message()
  200.                 self._input.pop_eof_matcher()
  201.                 while True:
  202.                     line = self._input.readline()
  203.                     if line is NeedMoreData:
  204.                         yield NeedMoreData
  205.                         continue
  206.                     
  207.                     break
  208.                 while True:
  209.                     line = self._input.readline()
  210.                     if line is NeedMoreData:
  211.                         yield NeedMoreData
  212.                         continue
  213.                     
  214.                     break
  215.                 if line == '':
  216.                     break
  217.                 
  218.                 self._input.unreadline(line)
  219.             return None
  220.         
  221.         if self._cur.get_content_maintype() == 'message':
  222.             for retval in self._parsegen():
  223.                 if retval is NeedMoreData:
  224.                     yield NeedMoreData
  225.                     continue
  226.                 
  227.                 break
  228.             
  229.             self._pop_message()
  230.             return None
  231.         
  232.         if self._cur.get_content_maintype() == 'multipart':
  233.             boundary = self._cur.get_boundary()
  234.             if boundary is None:
  235.                 self._cur.defects.append(errors.NoBoundaryInMultipartDefect())
  236.                 lines = []
  237.                 for line in self._input:
  238.                     if line is NeedMoreData:
  239.                         yield NeedMoreData
  240.                         continue
  241.                     
  242.                     lines.append(line)
  243.                 
  244.                 self._cur.set_payload(EMPTYSTRING.join(lines))
  245.                 return None
  246.             
  247.             separator = '--' + boundary
  248.             boundaryre = re.compile('(?P<sep>' + re.escape(separator) + ')(?P<end>--)?(?P<ws>[ \\t]*)(?P<linesep>\\r\\n|\\r|\\n)?$')
  249.             capturing_preamble = True
  250.             preamble = []
  251.             linesep = False
  252.             while True:
  253.                 line = self._input.readline()
  254.                 if line is NeedMoreData:
  255.                     yield NeedMoreData
  256.                     continue
  257.                 
  258.                 if line == '':
  259.                     break
  260.                 
  261.                 mo = boundaryre.match(line)
  262.                 if mo:
  263.                     if mo.group('end'):
  264.                         linesep = mo.group('linesep')
  265.                         break
  266.                     
  267.                     if capturing_preamble:
  268.                         if preamble:
  269.                             lastline = preamble[-1]
  270.                             eolmo = NLCRE_eol.search(lastline)
  271.                             if eolmo:
  272.                                 preamble[-1] = lastline[:-len(eolmo.group(0))]
  273.                             
  274.                             self._cur.preamble = EMPTYSTRING.join(preamble)
  275.                         
  276.                         capturing_preamble = False
  277.                         self._input.unreadline(line)
  278.                         continue
  279.                     
  280.                     while True:
  281.                         line = self._input.readline()
  282.                         if line is NeedMoreData:
  283.                             yield NeedMoreData
  284.                             continue
  285.                         
  286.                         mo = boundaryre.match(line)
  287.                         if not mo:
  288.                             self._input.unreadline(line)
  289.                             break
  290.                             continue
  291.                     self._input.push_eof_matcher(boundaryre.match)
  292.                     for retval in self._parsegen():
  293.                         if retval is NeedMoreData:
  294.                             yield NeedMoreData
  295.                             continue
  296.                         
  297.                         break
  298.                     
  299.                     if self._last.get_content_maintype() == 'multipart':
  300.                         epilogue = self._last.epilogue
  301.                         if epilogue == '':
  302.                             self._last.epilogue = None
  303.                         elif epilogue is not None:
  304.                             mo = NLCRE_eol.search(epilogue)
  305.                             if mo:
  306.                                 end = len(mo.group(0))
  307.                                 self._last.epilogue = epilogue[:-end]
  308.                             
  309.                         
  310.                     else:
  311.                         payload = self._last.get_payload()
  312.                         if isinstance(payload, basestring):
  313.                             mo = NLCRE_eol.search(payload)
  314.                             if mo:
  315.                                 payload = payload[:-len(mo.group(0))]
  316.                                 self._last.set_payload(payload)
  317.                             
  318.                         
  319.                     self._input.pop_eof_matcher()
  320.                     self._pop_message()
  321.                     self._last = self._cur
  322.                     continue
  323.                 preamble.append(line)
  324.             if capturing_preamble:
  325.                 self._cur.defects.append(errors.StartBoundaryNotFoundDefect())
  326.                 self._cur.set_payload(EMPTYSTRING.join(preamble))
  327.                 epilogue = []
  328.                 for line in self._input:
  329.                     if line is NeedMoreData:
  330.                         yield NeedMoreData
  331.                         continue
  332.                         continue
  333.                 
  334.                 self._cur.epilogue = EMPTYSTRING.join(epilogue)
  335.                 return None
  336.             
  337.             if linesep:
  338.                 epilogue = [
  339.                     '']
  340.             else:
  341.                 epilogue = []
  342.             for line in self._input:
  343.                 if line is NeedMoreData:
  344.                     yield NeedMoreData
  345.                     continue
  346.                 
  347.                 epilogue.append(line)
  348.             
  349.             if epilogue:
  350.                 firstline = epilogue[0]
  351.                 bolmo = NLCRE_bol.match(firstline)
  352.                 if bolmo:
  353.                     epilogue[0] = firstline[len(bolmo.group(0)):]
  354.                 
  355.             
  356.             self._cur.epilogue = EMPTYSTRING.join(epilogue)
  357.             return None
  358.         
  359.         lines = []
  360.         for line in self._input:
  361.             if line is NeedMoreData:
  362.                 yield NeedMoreData
  363.                 continue
  364.             
  365.             lines.append(line)
  366.         
  367.         self._cur.set_payload(EMPTYSTRING.join(lines))
  368.  
  369.     
  370.     def _parse_headers(self, lines):
  371.         lastheader = ''
  372.         lastvalue = []
  373.         for lineno, line in enumerate(lines):
  374.             if line[0] in ' \t':
  375.                 if not lastheader:
  376.                     defect = errors.FirstHeaderLineIsContinuationDefect(line)
  377.                     self._cur.defects.append(defect)
  378.                     continue
  379.                 
  380.                 lastvalue.append(line)
  381.                 continue
  382.             
  383.             if lastheader:
  384.                 lhdr = EMPTYSTRING.join(lastvalue)[:-1].rstrip('\r\n')
  385.                 self._cur[lastheader] = lhdr
  386.                 lastheader = ''
  387.                 lastvalue = []
  388.             
  389.             if line.startswith('From '):
  390.                 if lineno == 0:
  391.                     mo = NLCRE_eol.search(line)
  392.                     if mo:
  393.                         line = line[:-len(mo.group(0))]
  394.                     
  395.                     self._cur.set_unixfrom(line)
  396.                     continue
  397.                 elif lineno == len(lines) - 1:
  398.                     self._input.unreadline(line)
  399.                     return None
  400.                 else:
  401.                     defect = errors.MisplacedEnvelopeHeaderDefect(line)
  402.                     self._cur.defects.append(defect)
  403.             
  404.             i = line.find(':')
  405.             if i < 0:
  406.                 defect = errors.MalformedHeaderDefect(line)
  407.                 self._cur.defects.append(defect)
  408.                 continue
  409.             
  410.             lastheader = line[:i]
  411.             lastvalue = [
  412.                 line[i + 1:].lstrip()]
  413.         
  414.         if lastheader:
  415.             self._cur[lastheader] = EMPTYSTRING.join(lastvalue).rstrip('\r\n')
  416.         
  417.  
  418.  
  419.